#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>
#include "common.hpp"
#include "Log.hpp"
#include "InetAddr.hpp"
// 模版方法模式
// 基类socket, 大部分方法，都是纯虚方法
namespace SocketMoudle
{
    using namespace LogMoudle;
    const static int gbacklog = 16;
    class Socket
    {
    public:
        Socket()
        {
        }
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        //virtual std::shared_ptr<Socket> Accept(InetAddr *client) = 0;
        virtual int Accept(InetAddr *client) = 0;
        virtual void Close() = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &message) = 0;
        virtual int Connect(const std::string &server_ip, uint16_t port) = 0;
        virtual int Fd() = 0;
        virtual ~Socket()
        {
        }
        void BuildTcpSocketMethod(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }
        void BuildTcpClientSocketMethod()
        {
            SocketOrDie();
        }

    private:
    };
    class TcpSocket : public Socket
    {
    public:
        TcpSocket()
            : _sockfd(-1)
        {
        }
        TcpSocket(int fd) : _sockfd(fd)
        {
        }
        void SocketOrDie() override // 创建套接字
        {
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(loglevel::FATAL) << "socket error";
                exit(SOCKET_ERR);
            }
            LOG(loglevel::INFO) << "socket success";
        }
        void BindOrDie(uint16_t port) override // 绑定
        {
            InetAddr as(port);
            int n = bind(_sockfd, as.NetAddrPtr(), as.NetAddrLen());
            if (n < 0)
            {
                LOG(loglevel::FATAL) << "bind error";
                exit(BIND_ERR);
            }
            LOG(loglevel::INFO) << "bind success";
        }
        void ListenOrDie(int backlog) override // 设置听状态
        {
            int n = listen(_sockfd, backlog);
            if (n < 0)
            {
                LOG(loglevel::FATAL) << "listen error";
                exit(LISTEN_ERR);
            }
            LOG(loglevel::INFO) << "listen success";
        }
        int Accept(InetAddr *client) override // 获取连接
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int fd = accept(_sockfd, CONV(peer), &len);
            if (fd < 0)
            {
                LOG(loglevel::WARNING) << "accept warning ...";
                return -1;
            }
            client->SetAddr(peer);
            // return std::make_shared<TcpSocket>(fd);
            return fd;
        }
        void Close() override // 关闭文件描述符
        {
            if (_sockfd >= 0)
                close(_sockfd);
        }
        virtual int Recv(std::string *out) override // 读取
        {
            // 流式读取，不关心读到的是什么
            char buffer[1024];
            ssize_t n = recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if (n > 0)
            {
                buffer[n] = 0;
                *out += buffer; // 故意
            }
            return n;
        }
        virtual int Send(const std::string &message) override // 写入发送
        {
            return send(_sockfd, message.c_str(), message.size(), 0);
        }
        virtual int Connect(const std::string &server_ip, uint16_t port) override // 发送链接
        {
            InetAddr server(server_ip, port);
            return connect(_sockfd, server.NetAddrPtr(), server.NetAddrLen());
        }
        int Fd() override
        {
            return _sockfd;
        }
        ~TcpSocket()
        {
        }

    private:
        int _sockfd; // listensockfd
    };
}